package icu.d4peng.basic.core.json;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import icu.d4peng.basic.core.constant.DateTimeConstant;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * JacksonModule:jackson自定义序列化和反序列化规则
 *
 * @author d4peng <a href="d4peng@qq.com">d4peng@qq.com</a>
 * @since 2023.12.02
 */
public class JacksonModule extends SimpleModule {

	private static final long serialVersionUID = -777092083748155956L;

	public JacksonModule() {
		super();
		// 反序列化
		this.addDeserializer(LocalDateTime.class,
				new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateTimeConstant.NORM_DATE_TIME_PATTERN)));
		this.addDeserializer(LocalDate.class, new LocalDateDeserializer(
				DateTimeFormatter.ofPattern(DateTimeConstant.NORM_DATE_YEAR_MONTH_DAY_PATTERN)));
		this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(
				DateTimeFormatter.ofPattern(DateTimeConstant.NORM_TIME_HOUR_MINUTE_SECOND_PATTERN)));

		// 序列化
		this.addSerializer(LocalDateTime.class,
				new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateTimeConstant.NORM_DATE_TIME_PATTERN)));
		this.addSerializer(LocalDate.class, new LocalDateSerializer(
				DateTimeFormatter.ofPattern(DateTimeConstant.NORM_DATE_YEAR_MONTH_DAY_PATTERN)));
		this.addSerializer(LocalTime.class, new LocalTimeSerializer(
				DateTimeFormatter.ofPattern(DateTimeConstant.NORM_TIME_HOUR_MINUTE_SECOND_PATTERN)));
	}

}
